package com.kang.service.impl;

import com.kang.entity.Essay;
import com.kang.mapper.EssayMapper;
import com.kang.service.RedisToMySqlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RedisToMySqlServiceImpl implements RedisToMySqlService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EssayMapper essayMapper;

    @Override
    public void redisToMySql() {

        //点赞操作
        Set<String> likeKey = stringRedisTemplate.keys("user:like:".concat("*"));
        assert likeKey != null;
        for (String key : likeKey) {
            log.info("当前redis同步的key：" + key);
            List<String> stringList = Arrays.stream(key.split(":")).collect(Collectors.toList());
            String essayId = stringList.get(2);
            //找到数据库数据  更新操作  并删除key
            try {
                Essay essay = essayMapper.selectById(essayId);
                String s = stringRedisTemplate.opsForValue().get("user:like:" + essayId);
                assert s != null;
                essay.setClickNum(Integer.valueOf(s));
                essayMapper.updateById(essay);
            } catch (Exception ex) {
                log.error("更新点赞操作失败key:" + key, ex);
            }
        }

        //收藏操作
        Set<String> bookMarkKey = stringRedisTemplate.keys("user:bookMark:".concat("*"));
        assert bookMarkKey != null;
        for (String key : bookMarkKey) {
            log.info("当前redis同步的key：" + key);
            List<String> stringList = Arrays.stream(key.split(":")).collect(Collectors.toList());
            String essayId = stringList.get(2);
            //找到数据库数据  更新操作  并删除key
            try {
                Essay essay = essayMapper.selectById(essayId);
                String s = stringRedisTemplate.opsForValue().get("user:bookMark:" + essayId);
                assert s != null;
                essay.setFavorNum(Integer.valueOf(s));
                essayMapper.updateById(essay);
            } catch (Exception ex) {
                log.error("更新收藏操作失败key:" + key, ex);
            }
        }



        //浏览操作
        Set<String> viewKey = stringRedisTemplate.keys("user:like:".concat("*"));
        assert viewKey != null;
        for (String key : viewKey) {
            log.info("当前redis同步的key：" + key);
            List<String> stringList = Arrays.stream(key.split(":")).collect(Collectors.toList());
            String essayId = stringList.get(2);
            //找到数据库数据  更新操作  并删除key
            try {
                Essay essay = essayMapper.selectById(essayId);
                String s = stringRedisTemplate.opsForValue().get("user:view:" + essayId);
                assert s != null;
                essay.setViewNum(Integer.valueOf(s));
                essayMapper.updateById(essay);
            } catch (Exception ex) {
                log.error("更新浏览操作失败key:" + key, ex);
            }
        }



        //评论数
        Set<String> replyKey = stringRedisTemplate.keys("user:reply:".concat("*"));
        assert replyKey != null;
        for (String key : replyKey) {
            log.info("当前redis同步的key：" + key);
            List<String> stringList = Arrays.stream(key.split(":")).collect(Collectors.toList());
            String essayId = stringList.get(2);
            //找到数据库数据  更新操作  并删除key
            try {
                Essay essay = essayMapper.selectById(essayId);
                String s = stringRedisTemplate.opsForValue().get("user:reply:" + essayId);
                assert s != null;
                essay.setReplayNum(Integer.valueOf(s));
                essayMapper.updateById(essay);
            } catch (Exception ex) {
                log.error("更新评论数失败key:" + key, ex);
            }
        }

    }
}
